#!/usr/bin/env python

'''
Each method in this module reads in a text file in the sink files, print_agn files, halo list, etc.
'''

def load_sinkdata(sinklogfile,column_names,quantities=None,delta_t=0.0) :
    '''
    Reads in timeordered sinklogfile (single BH file)
    Returns some specified quantities (i.e. ['aexp','cs','rhog','T','Tsink'])
    Ex:
    sinkdata = load_sinkdata(timeordered_11111.dat,quantities=['aexp','cs'],delta_t=10)
    plot(sinkdata['aexp'],sinkdata['cs']) # Plots sound speed vs. aexp with sound speed averaged over every 10Myr
    '''
    inputfile = open( sinklogfile, 'r' )
    columns = open( column_names, 'r')

    #Reads the column names from column_names file.
    #Col names delimited by white spaces, doesn't matter
    #if the names span multiple lines.
    s = []
    for line in columns: 
        st = (line.lower()).split() #case insensitive
        s += st
    #So s looks like [particle_id, aexp, dld_macc[msun/myr], ...]
        

##OLD
    # Dictionary of corresponding column values
    # data_col = { 'aexp':0, 'Mbh':12
    #             }
    # Dictionary of arrays to return
    # data = { 'aexp':[], 'Mbh':[]
    #         }

    #while 1 : # Skip over header
    #     line = inputfile.readline()
    #     if line[0] == '#' :
    #         break
##END OLD
    
    if quantities == None :
        quantities = s
#    else : #Why else? If specific quantities are not specified, you don't want delta_t in the dict?
#        quantities.append('delta_t')
    for quantity in quantities:
        quantity.lower()
        
    data = {quantity:[] for quantity in quantities} #Makes a dict with only the specified quantities
    

    for line in inputfile :  ### NEED TO MAKE THIS TIME AVERAGED OVER SOME dt ###
        if line == "":
            continue
        if not line.startswith('#') :
            cols = (line.lower()).split() #Splits each line into an array
            for quantity in quantities :
                data[quantity].append(float(cols[s.index(quantity)]))
                #Ex. data['particle_id'].append(...)
                #s.index('particle_id') would be 0, as provided by the column_names file
                #so cols[0] would be the particle id, and it would be appended to data['particle_id']

    return data

def load_agnlist(agnlist) :
    pass
    


